/**
 * 监听器
 * @param {Object} data vue数据对象
 */
 function observe(data) {
  if(!data || typeof data != 'object') {
    return ;
  }
  Object.keys(data).forEach(function(key) {
    defineReactive(data, key, data[key]);
  })
}
 
function defineReactive(data, key, val) {
  observe(val);
  var dep = new Dep();
  var property = Object.getOwnPropertyDescriptor(data, key);
  var getter = property && property.get;
  var setter = property && property.set;
  Object.defineProperty(data,key,{
    configurable: true,
    enumerable: true,
    set: function(newVal) {
	  //var value = getter ? getter.call(data) : val;
      if (setter) {
        setter.call(data, newVal);
      } else {
        val = newVal;
      }
	  //val = newVal;
      //console.log('属性：' + key + '已经被监听，现在的值为：'+ newVal);
      dep.notify();
    },
    get: function() {
	   var value = getter?.call(data) || val;
	  // var value = val;
	  // console.log('属性：' + key + '已经被读取' + getter);
      if (Dep.target) {
        dep.subs.push(Dep.target);
      }
	  console.log(key + '的dept合积极：',  dep.subs);
      return value;
    }
  })
}

/**
 * 订阅器
 */
function Dep() {
  this.subs = [];
  this.target = null;
}
 
Dep.prototype = {
  constructor: Dep,
  addSub: function(sub) { // 添加订阅者
    this.subs.push(sub);
  },
  notify: function() { // 通知订阅者
    this.subs.forEach(function(sub) {
      sub.update();
    })
  }
}